package a66weding.com.jiehuntong.wxapi;


import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.common.MessageEvent;
import a66weding.com.jiehuntong.inner.PayCallBack;
import a66weding.com.jiehuntong.wx.Constants;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private int error_code = 10000;
    private IWXAPI api;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID, false);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            this.error_code = baseResp.errCode;
            MessageEvent messageEvent = new MessageEvent();
            messageEvent.setPayCode(this.error_code);
            EventBus.getDefault().post(messageEvent);

            if (error_code == 0) {
                Toast.makeText(getApplicationContext(), "支付结果：成功", Toast.LENGTH_SHORT).show();
            } else if (error_code == -2) {
                Toast.makeText(getApplicationContext(), "支付结果：取消操作", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "支付结果：失败", Toast.LENGTH_SHORT).show();
            }

            this.finish();
        }
    }


}